package com.yigerende.controller;


import com.yigerende.feign.OrderFeign;
import com.yigerende.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;

    //管理员的订单页
    @GetMapping("jr")
    public String index(){
        return "order_handler";
    }

    //用户的订单页面
    @GetMapping("jr1")
    public String index1(){
        return "order";
    }



    //添加订单
    @GetMapping("/save/{mid}")
    public String save(@PathVariable("mid") Integer mid, HttpSession session){
        //获取当前下单用户
        Account user = (Account) session.getAttribute("user");
        //获取到下单的菜单id
        TMenu menu = new TMenu();
        menu.setId(mid);
        //传递的订单对象
        TOrder order = new TOrder();
        TUser user1 = new TUser();
        user1.setId(user.getId());
        order.setTUser(user1);
        order.setTMenu(menu);
        order.setDate(new Date());
        orderFeign.save(order);
        return "redirect:/order/jr1";
    }

    //根据登陆用户来显示它的订单
    @GetMapping("/findAllByUid")
    @ResponseBody
    public OrderVo findAllByUid(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, HttpSession session){
        //获取到用户id
        Account user = (Account) session.getAttribute("user");
        OrderVo f =orderFeign.findAllByUid(user.getId(), page, limit);
        return orderFeign.findAllByUid(user.getId(), page, limit);
    }


    //通过用户是否派送查询数据(分页)(map传递state,start,end)(查询未派送的订单)
    @GetMapping("/findAllByState")
    @ResponseBody
    public OrderVo findAllByState(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        return orderFeign.findAllByState(0, page, limit);
    }


    //那个管理员审核的，审核修改aid对应管理员和state状态
    @GetMapping("/updateState/{id}/{state}")
    public String updateState(@PathVariable("id") Integer id,@PathVariable("state") Integer state,HttpSession session){
        Account admin = (Account) session.getAttribute("admin");
        orderFeign.updateState(id,admin.getId(),state);
        return "redirect:/order/jr";
    }
}
